package com.taiyuan.tongcheng.ui;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.kymjs.kjframe.ui.BindView;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.StringUtils;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taiyuan.tongcheng.application.AppConfig;
import com.taiyuan.tongcheng.bean.ServerStation;
import com.taiyuan.tongcheng.db.TcDbHelper;
import com.taiyuan.tongcheng.util.SharePreferenceUtil;
import com.taiyuan.tongcheng.util.TcUtil;
import com.tytc56.user.R;

public class StationInputActivity extends BaseActivity{
//	widget
	@BindView(id=R.id.etv_connect_name)
	private EditText staConnectName;
	@BindView(id=R.id.etv_connect_tel)
	private EditText staConnectTel;
	@BindView(id=R.id.rl_map_select,click=true)
	private RelativeLayout mapSelectRl;
	@BindView(id=R.id.tv_input_add)
	private TextView  staAddress;
	@BindView(id=R.id.btn_sub,click=true)
	private Button okBtn;
	@BindView(id=R.id.btn_use_sendinfo,click=true)
	private Button useCurrentInfo;
	
	//data
	private ServerStation staInfo=new ServerStation();
	private ServerStation comeStaInfo=new ServerStation();
	private String currentString;
	//rsq intent
	private Intent inputIntent;
	public  int comeCode=1;
	private int halfPositon=0;
	private Intent addressSelectIntent;
	//Util
	private SharePreferenceUtil spUtil;
	

	@Override
	public void setRootView() {
		setContentView(R.layout.activity_input_station_info);
	}
	
	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
		
		case R.id.rl_map_select:
			Bundle adbundle=new Bundle();
			adbundle.putSerializable(ExpressActivity.TAG_STATION_INFO,comeStaInfo );
			adbundle.putString(AddressSelectActivity.TAG_MAP_INPUT, currentString);
			addressSelectIntent.putExtras(adbundle);
			startActivityForResult(addressSelectIntent, 1);
			break;
			
		case R.id.btn_use_sendinfo:
				staConnectName.setText(spUtil.getSendUserName());
				staConnectTel.setText(spUtil.getSendUserPhone());
				comeStaInfo.setReceiver(spUtil.getSendUserName());
				comeStaInfo.setReceiverMobile(spUtil.getSendUserPhone());
				
			break;
		case R.id.btn_sub:
			if(checkInput()){
				comeStaInfo.setReceiver(staConnectName.getText().toString());
				comeStaInfo.setReceiverMobile(staConnectTel.getText().toString());
				if(comeCode==ExpressActivity.RES_CODE_START){
					spUtil.setSendUserName(comeStaInfo.getReceiver());
					spUtil.setSendUserPhone(comeStaInfo.getReceiverMobile());
				}
				
				Bundle bundle=new Bundle();
				bundle.putSerializable("STA_POI", comeStaInfo);
				bundle.putInt("POSITION", halfPositon);
				inputIntent.putExtras(bundle);
				setResult(RESULT_OK, inputIntent);
				StationInputActivity.this.finish();
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public void initWidget() {
		initView();
		super.initWidget();
		inputIntent=getIntent();
		spUtil=new SharePreferenceUtil(getApplicationContext(), AppConfig.SHARE_NAME);
		halfPositon=getIntent().getIntExtra("POSITION", 0);
		comeStaInfo=(ServerStation)getIntent().getSerializableExtra(ExpressActivity.TAG_STATION_INFO);
		refreashUI();
		addressSelectIntent =new Intent(StationInputActivity.this,AddressSelectActivity.class);
	}
	
	private void refreashUI() {
		staAddress.setText(comeStaInfo.getPlace());
		staConnectName.setText(comeStaInfo.getReceiver());
		staConnectTel.setText(comeStaInfo.getReceiverMobile());
	}

	@Override
	protected void initView() {
		super.initView();
		comeCode=getIntent().getIntExtra(ExpressActivity.TAG_ORDER_REQ_CODE, 0);
		rightTv.setVisibility(View.GONE);
		switch (comeCode) {
		case ExpressActivity.RES_CODE_START:
			topTv.setText(R.string.lable_send_info);
			staAddress.setHint(R.string.station_send_address);
			staConnectName.setHint(R.string.station_send_connect_name);
			currentString=getString(R.string.station_send_address);
			break;
		case ExpressActivity.RES_CODE_END:
			useCurrentInfo.setVisibility(View.VISIBLE);
			topTv.setText(R.string.lable_receive_info);
			staAddress.setHint(R.string.station_receive_address);
			staConnectName.setHint(R.string.station_receive_connect_name);
			currentString=getString(R.string.station_receive_address);
			break;
		case  ExpressActivity.RES_CODE_HALF:
			useCurrentInfo.setVisibility(View.VISIBLE);
			staAddress.setHint(R.string.station_receive_address);
			staConnectName.setHint(R.string.station_receive_connect_name);
			topTv.setText(R.string.station_receive_address);
			currentString=getString(R.string.station_receive_address);
			break;
		default:
			break;
		}
	}
	
	private boolean  checkInput() {
		if(StringUtils.isEmpty(staAddress.getText().toString())){
			showErroCustomToast("请输入地址");
			   return false ;	
		}
		if(StringUtils.isEmpty(staConnectName.getText())){
			ViewInject.toast(this, getString(R.string.station_input_name));
		   return false ;	
		}
		if(StringUtils.isEmpty(staConnectTel.getText())){
			ViewInject.toast(this, getString(R.string.station_input_tel));
			return false;
		}
		
		if(!TcUtil.isMobileNO(staConnectTel.getText().toString())){
			showErroCustomToast("请输入正确的电话号码");
			return false;
		}
		return true;
	}
	
	

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		if(arg1==RESULT_OK){
			staInfo=(ServerStation)arg2.getSerializableExtra("MAP_POI");
			staAddress.setText(staInfo.getPlace());
			comeStaInfo.setPlace(staInfo.getPlace());
			comeStaInfo.setLatitude(staInfo.getLatitude());
			comeStaInfo.setLongitude(staInfo.getLongitude());
			comeStaInfo.setProvince(staInfo.getProvince());
			comeStaInfo.setCity(staInfo.getCity());
			comeStaInfo.setCityCode(spUtil.getLocationCityCode());
		}
	}
	
	public static boolean isMobileNum(String mobiles) {
		 Pattern p = Pattern
		 .compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))|(14[0-9])|(17[0-9])\\d{8}$");
		 Matcher m = p.matcher(mobiles);
		 System.out.println(m.matches() + "---");
		 return m.matches();

		 }
}
